﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Common.CommandTrees;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Web.Http;
using WordsOfLoveServer.Models;
using WordsOfLoveServer.Models.Forums;

namespace WordsOfLoveServer.Controllers
{

	public class UserController : ApiController
	{
		#region User APIs
		// GET: api/User/Validate?username=xxx&password=xxx
		[HttpGet]
		public HttpResponseMessage Validate(string username, string password)
		{
			try
			{
				using (var db = new WOLDbContext())
				{
					IQueryable<User> users = db.Users.Where(x => x.UserName == username && x.Password == password);
					User user = users.Count() == 0 ? null : users.First();
					return new HttpResponseMessage()
					{
						RequestMessage = Request,
						Content = new StringContent(JsonConvert.SerializeObject(user))
					};
				}
			}
			catch (Exception ex)
			{
				return new HttpResponseMessage(HttpStatusCode.BadRequest)
				{
					Content = new StringContent(ex.Message)
				};
			}
		}

		// POST: api/User/CreateSentence
		[HttpPost]
		public HttpResponseMessage CreateUser([FromBody] User user)
		{
			try
			{
				using (var db = new WOLDbContext())
				{
					db.Users.Add(user);
					db.SaveChanges();
				}
				return new HttpResponseMessage(HttpStatusCode.Created);
			}
			catch (Exception ex)
			{
				return new HttpResponseMessage(HttpStatusCode.BadRequest)
				{
					Content = new StringContent(ex.Message)
				};
			}
		}

		// PUT: api/User/UpdateUser
		[HttpPut]
		public HttpResponseMessage UpdateUser([FromBody] User user)
		{
			try
			{
				using (var db = new WOLDbContext())
				{
					db.Entry(user).State = EntityState.Modified;
					db.SaveChanges();
				}
				return new HttpResponseMessage(HttpStatusCode.OK);
			}
			catch (Exception ex)
			{
				return new HttpResponseMessage(HttpStatusCode.BadRequest)
				{
					Content = new StringContent(ex.Message)
				};
			}
		}

		//DELETE: api/User/DeleteUser?id=xxx
		[HttpDelete]
		public HttpResponseMessage DeleteUser(int id)
		{
			try
			{
				using (var db = new WOLDbContext())
				{
					User user = db.Users.Find(id);
					if (user == null)
					{
						return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Item doesn't exist.") };
					}
					db.Users.Remove(user);
					db.SaveChanges();
					return new HttpResponseMessage(HttpStatusCode.OK);
				}
			}
			catch (Exception ex)
			{
				return new HttpResponseMessage(HttpStatusCode.BadRequest)
				{
					Content = new StringContent(ex.Message)
				};
			}
		}

		#endregion

		#region TO BE DELETED
		[HttpGet]
		public HttpResponseMessage GetAllUsers()
		{
			try
			{
				using (var db = new WOLDbContext())
				{
					return new HttpResponseMessage()
					{
						RequestMessage = Request,
						Content = new StringContent(JsonConvert.SerializeObject(db.Users))
					};
				}
			}
			catch (Exception ex)
			{
				return new HttpResponseMessage(HttpStatusCode.BadRequest)
				{
					Content = new StringContent(ex.Message)
				};
			}
		}
		#endregion
	}
}
